// add namespacing capability ala YUI
jQuery.namespace = function () {
    var a = arguments, o = null, i, j, d;
    for (i = 0; i < a.length; i = i + 1) {
        d = a[i].split(".");
        o = window;
        for (j = 0; j < d.length; j = j + 1) {
            o[d[j]] = o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o;
};
// add the g (games) namespace, can be used like jQuery.g.foo = { ... }
jQuery.namespace('jQuery.g');
jQuery.ajaxSetup({ cache: false });

/**** begin common object ******/
jQuery.g.common = function (options) {

    this.settings = jQuery.extend({
        errorSelector: '#error',
        errorMessageSelector: '#errorMessage',
        errorDelay: 8000,
        infoSelector: '#info',
        infoMessageSelector: '#infoMessage',
        infoDelay: 6000,
        loadingSelector: '#loader',
        postbackErrorMessageSelector: '#postbackErrorMessage',
        postbackInfoMessageSelector: '#postbackInfoMessage'
    }, options);

    this.init();
};
jQuery.g.common.instance = null;
jQuery.g.common.prototype.settings = null;
jQuery.g.common.prototype.defaultSuccess = function (data) {
    jQuery.g.common.instance.showInfo('Success.');
};
jQuery.g.common.prototype.defaultError = function (data) {
    jQuery.g.common.instance.showError('Error.');
};
jQuery.g.common.prototype.showInfo = function (message, showAlert) {
    var self = this;
    self.hideMessages();
    self.hideLoading();
    if (showAlert) {
        alert(message);
    }
    else {
        jQuery(self.settings.infoMessageSelector).text(message);
        jQuery(self.settings.infoSelector).show('slow');
        self.scrollTop();
    }
};
jQuery.g.common.prototype.showError = function (message) {
    var self = this;
    self.hideMessages();
    self.hideLoading();
    jQuery(self.settings.errorMessageSelector).text(message);
    jQuery(self.settings.errorSelector).show('slow');
    self.scrollTop();
};
jQuery.g.common.prototype.checkResponseForError = function (responseData) {
    if (responseData && responseData.Error && responseData.Error.length > 0) {
        this.showError(responseData.Error);
        return false;
    } else {
        return true;
    }
};
jQuery.g.common.prototype.hideMessages = function () {
    jQuery(this.settings.infoSelector).hide();
    jQuery(this.settings.errorSelector).hide();
};
jQuery.g.common.prototype.scrollTop = function () {
    var whatToScroll = 'body';
    if (jQuery.browser.msie) {
        whatToScroll = 'html';
    }
    jQuery(whatToScroll).animate({ scrollTop: 0 }, 'fast');
};
jQuery.g.common.prototype.displayPostbackInfoIfAny = function () {
    var posbackMessage = jQuery(this.settings.postbackInfoMessageSelector);
    if (posbackMessage.length == 1) {
        this.showInfo(posbackMessage.val());
    }
};
jQuery.g.common.prototype.displayPostbackErrorIfAny = function () {
    var posbackMessage = jQuery(this.settings.postbackErrorMessageSelector);
    if (posbackMessage.length == 1) {
        this.showError(posbackMessage.val());
    }
};
jQuery.g.common.prototype.displayPostbackMessagesIfAny = function () {
    this.displayPostbackErrorIfAny();
    this.displayPostbackInfoIfAny();
};
jQuery.g.common.prototype.showLoading = function () {
    var self = jQuery.g.common.instance;
    jQuery(self.settings.loadingSelector).show();
    self.hideMessages();
};
jQuery.g.common.prototype.hideLoading = function () {
    var self = jQuery.g.common.instance;
    jQuery(self.settings.loadingSelector).hide();
};

jQuery.g.common.prototype.init = function () {
    var self = this;

    // set up catch all error handler
    jQuery(this.settings.errorSelector).ajaxError(function (event, request, options, thrownError) {
        self.hideLoading();
        self.defaultError();
    });

    jQuery(this.settings.loadingSelector)
        .hide()
        .ajaxStart(self.showLoading)
        .ajaxStop(self.hideLoading);

    // set up the error and info boxes to hide when they're clicked
    jQuery(self.settings.errorSelector).click(function () {
        self.hideMessages();
    });
    jQuery(self.settings.infoSelector).click(function () {
        self.hideMessages();
    });

    // set up the global instance reference to point to this
    jQuery.g.common.instance = this;
};
/**** end common object ******/
